package org.example.util;

import java.util.Formatter;

public class HexUtil {
    public static String hexFormat(byte[] bytes,HexFormat format){
        String separator = format.separator;
        int column = format.column;
        return hexFormat(bytes,separator,column);
    }

    private static String hexFormat(byte[] bytes, String separator, int column) {
        if(bytes == null || bytes.length < 1) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        Formatter fm = new Formatter(sb);

        int length = bytes.length;
        for (int i = 0; i < length; i++) {
            int val = bytes[i] & 0xFF;
            fm.format("%02X",val);
            if(column > 0 && (i+1) % column == 0){
                fm.format("%n");
            }else{
                fm.format("%s",separator);
            }
        }

        return sb.toString();
    }
}
